#include <stdio.h>
#include <string.h>
#include <getopt.h>
#include <unistd.h>
#include "testopt.h"

 struct option long_options[] = {
	{"help", 0,    NULL,  0    },   /*case 0*/
	{ "in",  1,    NULL,  0    },   /*case 1*/
	{ "out", 1,    NULL,  0    },   /*case 2*/
	{"ref",  1,    NULL,  0    },   /*case 3*/
	{"play", 1,    NULL,  0    },   /*case 4*/
	{"samplerate",1,NULL,  0    },  /*case 5*/
	{"frmnum", 1,NULL, 0},           /*case 6*/
	{"numperfrm",1, NULL,  0    },    /*case 7*/
	{"num", 1,  NULL,  0},             /*case 8*/  /*record time*/
	{"vol", 1,  NULL, 0 },            /*case 9*/
	{"gain",1,NULL,0},               /*case 10*/
	{"agc", 1, NULL,0},             /*case 11*/
	{"ns",1,NULL,0},               /*case 12*/
	{"hpf",1,NULL,0},             /*case 13*/
	{"type",1,NULL,0},        /*case 14*/
	{ 0,0,0,0},
};

TEST_ARGS testArgs;  /*save option args*/
char fileNameBuf[NAME_BUF_SIZE];  /*save varity file name;*/

/*get name of current execute file*/
int getCurrentElfPathName(char* processdir,char* processname,int pathLen)
{
	if(NULL == processname||NULL == processname ||pathLen <= 0){
		printf("fun:%s,input pararam in not invalid",__func__);
		return -1;
	}

	char* path_end = NULL;
	if(readlink("/proc/self/exe", processdir,pathLen) <=0)
		return -1;

	path_end = strrchr(processdir,  '/');
	if(path_end == NULL)
		return -1;

	++path_end;

	strcpy(processname, path_end);
	*path_end = '\0';

	return (int)(path_end - processdir);
}

int show_helpInfo()
{
	char path[MAX_DIR_PATH_LEN] = {0};
	char processname[MAX_ELF_NAME_LEN] = {0};

	getCurrentElfPathName(path,processname,sizeof(path));
	printf("*****************Help Information Of Test Program************************\n");
	printf("Usage: ./%s [OPTION] value\n",processname);
	printf("Options are as follows:\n");

	if(!strcmp("test_ao",processname)||!strcmp("test_ai",processname)){
		printf("******Options******        *********Comments*******************\n\n");
		printf("--in filename              set filename of audio file to play\n");
		printf("--out filename             set filename of audio file to record\n");
		printf("--samplerate value         set samplerate of audio record or play\n");
		printf("--frmnum value             set frmNum of audio record or play\n");
		printf("--numperfrm value          set numPerFrm  of audio record or play\n");
		printf("--num value                set time of recording audio(In seconds)\n");
		printf("--vol value                set volume of audio record or play\n");
		printf("--gain value               set gain of audio record or play\n");
		printf("--agc value1:value2        set agc parameter of audio record or play\n");
		printf("--ns value                 set ns  parameater of audio record or play\n");
		printf("--hpf value                set hpf enable flag aof audio record or play\n\n");
		printf("****Using example****\n");
		if(!strcmp("test_ao",processname))
			printf("./%s --in test_ao.pcm --samplerate 8000 --hpf 1 --agc 0:9\n\n",processname);
		else
			printf("./%s --out test_ai.pcm --samplerate 8000 --hpf 1 --agc 0:9 --ns 3\n\n",processname);
	}

	if(!strcmp("test_ai_ref",processname)){
		printf("******OPTIONS******        *********Comments*******************\n");
		printf("--out filename             set output filename of ref testing\n");
		printf("--ref filename             set ref filename of ref testing\n");
		printf("--play filename            set play filename of ref testing.\n");
		printf("--samplerate value         set samplerate of audio record or play\n");
		printf("--frmnum value             set frmNum of audio ref test\n");
		printf("--numperfrm value          set numPerFrm  of audio record or play\n");
		printf("--vol value                set volume of audio ref test play\n");
		printf("--gain value               set gain of audio ref test play\n");
		printf("--num value                set REF test record time of audio aec test(In seconds)\n");
	}

	if(!strcmp("test_ap_agc",processname)) {
		printf("******OPTIONS******        *********Comments*******************\n");
		printf("--in filename              set input filename of AGC testing\n");
		printf("--out filename             set output filename of AGC testing\n");
		printf("--agc param1:param2        set AGC parameter of AGC testing\n");
		printf("--samplerate value             set input file samplerate of Agc testing\n");
		printf("****Using example****\n");
		printf("./%s --in test_agc.pcm --samplerate 8000 --out test_agc_result.pcm\n\n",processname);
	}

	if(!strcmp("test_ap_hpf",processname)) {
		printf("******OPTIONS******        *********Comments*******************\n");
		printf("--in filename              set input filename of HPF testing\n");
		printf("--out filename             set output filename of HPF testing\n");
	}

	if(!strcmp("test_ap_ns",processname)) {
		printf("******OPTIONS******        *********Comments*******************\n");
		printf("--in filename              set input filename of NS testing\n");
		printf("--out filename             set output filename of NS testing\n");
		printf("--samplerate value         set input filename samplerate of NS testing\n");
		printf("--ns nsMode                set NS parameter of NS testing\n");
	}

	if(!strcmp("test_ap_aec",processname)){
		printf("******OPTIONS******        *********Comments*******************\n");
		printf("--play filename            set filename for aec test play\n");
		printf("--samplerate value         set audio file samplerate of aec test play\n");
		printf("--frmnum value             set frmNum of aec test play file\n");
		printf("--numperfrm value          set numPerFrm  of aec test play file\n");
		printf("--out  filename            set filename for aec test out\n");
		printf("--vol value                set volume of audio test play\n");
		printf("--gain value               set gain of audio test play\n");
		printf("--num value                set AEC record time of audio aec test(In seconds)\n");
	}
	if(!strcmp("test_aenc",processname)){
		printf("******OPTIONS******        *********Comments*******************\n");
		printf("--in ai|filename           set input filename fo AENC(ai or pcm file)\n");
		printf("--out filename             set output filename for AENC(adpcm/g711/g726)\n");
		printf("--type value               set type of aenc(0:adpcm,1:g711a,2:g711u,2:g726)\n");
	}

	if(!strcmp("test_adec",processname)){
		printf("******OPTIONS******        *********Comments*******************\n");
		printf("--in filename              set input filename fo ADEC(file of adpcm or g711 or g726)\n");
		printf("--out ao|filename          set output filename for ADEC(adpcm/g711/g726)\n");
		printf("--type value               set type of aenc(0:adpcm,1:g711a,2:g711u,3:g726)\n");
	}

	printf("***************************The End***************************************\n");

	return 0;
}

static int init_testArgs()
{
	memset(fileNameBuf,0x0,NAME_BUF_SIZE);
	memset(&testArgs,0x0,sizeof(TEST_ARGS));

	testArgs.inFileName = fileNameBuf;
	testArgs.outFileName = fileNameBuf + MAX_FILE_NAME_LEN;
	testArgs.refFileName = fileNameBuf + 2*MAX_FILE_NAME_LEN;
	testArgs.playFileName = fileNameBuf + 3*MAX_FILE_NAME_LEN;

	strncpy(testArgs.inFileName,"",MAX_FILE_NAME_LEN);
	strncpy(testArgs.outFileName,"",MAX_FILE_NAME_LEN);
	strncpy(testArgs.refFileName,"",MAX_FILE_NAME_LEN);
	strncpy(testArgs.playFileName,"",MAX_FILE_NAME_LEN);

	testArgs.sampleRate = 0;

	testArgs.frmNum = 0;
	testArgs.numPerFrm = 0;
	testArgs.num = 10;

	testArgs.vol = 60;  /*The same to default vol value*/
	testArgs.gain = 28;

	/*Agc/Hpf/Ns/Aec*/
	testArgs.nsMode = -1;
	testArgs.hpfFlag = 0;

	(testArgs.agcConfig).TargetLevelDbfs = -1;
	(testArgs.agcConfig).CompressionGaindB = -1;

	testArgs.type = -1; /*set enc or denc type*/

	return 0;

}

int parse_param(int argc,char **argv)  /*parse param from input*/
{
	int ret = -1;
	int c = 0;
	int option_index = 0;
	int tmptype = 0;

	ret = init_testArgs();
	if(ret != 0){
		printf("fun:%s,init_testArgs failed\n",__func__);
		return -1;
	}

	while(1) {
		c = getopt_long(argc,argv,"",long_options,&option_index);
		if(-1 == c) {
			break;
		}
		if('?' == c) {
			return -1;
		}
		switch(option_index) {
			case 0:
				show_helpInfo();
				return 1;
			case 1:
				ret = sscanf(optarg,"%s",testArgs.inFileName);
				break;
			case 2:
				ret = sscanf(optarg,"%s",testArgs.outFileName);
				break;
			case 3:
				ret = sscanf(optarg,"%s",testArgs.refFileName);
				break;
			case 4:
				ret = sscanf(optarg,"%s",testArgs.playFileName);
				break;
			case 5:
				ret = sscanf(optarg,"%d",&(testArgs.sampleRate));
				break;
			case 6:
				ret = sscanf(optarg,"%d",&(testArgs.frmNum));
				break;
			case 7:
				ret = sscanf(optarg,"%d",&(testArgs.numPerFrm));
				break;
			case 8:
				ret = sscanf(optarg,"%d",&(testArgs.num));
				break;
			case 9:
				ret = sscanf(optarg,"%d",&(testArgs.vol));
				break;
			case 10:
				ret = sscanf(optarg,"%d",&(testArgs.gain));
				break;
			case 11:
				ret = sscanf(optarg,"%d:%d",&((testArgs.agcConfig).TargetLevelDbfs),
											&((testArgs.agcConfig).CompressionGaindB));
				break;
			case 12:
				ret = sscanf(optarg,"%d",&(testArgs.nsMode));
				break;
			case 13:
				ret = sscanf(optarg,"%d",&(testArgs.hpfFlag));
				break;
			case 14:
				ret = sscanf(optarg,"%d",&(testArgs.type));
			default:
				printf("info:getopt_long return c %d\n",c);
				break;
		}
	}
	return 0;
}

